// pages/login/index.js
const app = getApp()
Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		tabIndex: 0, //tab active
		phone: '',
		password: '',
		initCountDown: app.globalData.initCountDown, //原始倒计时
		countDown: app.globalData.initCountDown, //倒计时
		phone2: '',
	},
	changTab(e) {
		let index = e.target.dataset.index;
		this.setData({
			tabIndex: index
		})

	},
	//获取验证码
	onGetCode() {
		let phone = this.data.phone2;
		let that = this;
		if (!phone || phone.length <= 0) {
			wx.showToast({
				title: "请输入手机号",
				icon: 'none',
				duration: 2000
			})
			return false
		}

		if (!(/^1[345789]\d{9}$/.test(phone))) {
			wx.showToast({
				title: "请输入正确的手机号",
				icon: 'none',
				duration: 2000
			})
			return false
		}
		this.onCodeDown()
		wx.request({
			url: app.globalData.baseUrl + '/api/getVerifyCode',
			method: 'GET',
			header: {
				'content-type': 'application/x-www-form-urlencoded',
				'token': app.globalData.token
			},
			data: {
				"phone": phone,
			},
			success: function(res) {
				if (res.data.status) {

					// app.globalData.is_finish_wx = res.data.data.records.is_finish_wx;
					// app.globalData.phone = res.data.data.records.phone;



				}

			},
			fail: function() {

				console.log("请求失败")
			}
		})
	},
	//绑定手机号动态
	getPhone(e) {
		this.setData({
			phone2: e.detail.value
		})
	},
	//验证阿妈发送中
	onCodeDown() {
		let that = this;
		let time = that.data.countDown;
		let setTime;
		clearInterval(setTime)
		time--;
		that.setData({
			countDown: time
		})
		setTime = setInterval(() => {
			time--;
			console.log
			if (time <= 0) {
				that.setData({
					countDown: that.data.initCountDown
				})
				clearInterval(setTime);
			} else {
				that.setData({
					countDown: time
				})
			}
		}, 1000)
  },
  phoneChange(e){
      console.log(e);
      this.setData({
        phone:e.detail.value
      })
  },
  passwordChange(e){
    this.setData({
      password:e.detail.value
    })
},
	//登录
	onLogin(e) {
		// let phone = e.detail.value.phone;
    // let password = e.detail.value.password;
    let phone =this.data.phone;
		let password =this.data.password;

		if (!phone || phone.length <= 0) {
			wx.showToast({
				title: "请输入手机号",
				icon: 'none',
				duration: 2000
			})
			return false
		}
		if (!password || password.length <= 0) {
			wx.showToast({
				title: "请输入密码",
				icon: 'none',
				duration: 2000
			})
			return false
		}
		if (!(/^1[345789]\d{9}$/.test(phone))) {
			wx.showToast({
				title: "请输入正确的手机号",
				icon: 'none',
				duration: 2000
			})
			return false
		}
		wx.showLoading({
			title: '提交中',
			mask: true
		})
		wx.request({
			url: app.globalData.baseUrl + '/api/loginWithPassword',
			method: 'POST',
			header: {
				'content-type': 'application/x-www-form-urlencoded',
			},
			data: {
				"phone": phone,
				"password": password
			},
			success: function(res) {
				wx.hideLoading()
				if (res.data.status) {
					app.globalData.token = res.data.data.records.token;
					// app.globalData.is_finish_wx = res.data.data.records.is_finish_wx;
					// app.globalData.phone = res.data.data.records.phone;
					wx.setStorage({
						key: "time",
						data: new Date().getTime(),
						success() {

						}
					})
					wx.setStorage({
						key: "token",
						data: res.data.data.records.token,
						success() {
                // //请求个人信息，判断是否跳转到webview授权页面
                wx.request({
                  url: app.globalData.baseUrl + '/api/user/getUserInfo',
                  method: 'GET',
                  header: {
                    'content-type': 'application/x-www-form-urlencoded',
                    'token': res.data.data.records.token,
                  },
                  success: function(res) {
                    if (res.data.status) {
                      app.globalData.records = res.data.data.records;
                      wx.setStorage({
                        key: "records",
                        data: JSON.stringify(res.data.data.records)
                      })
                      if(!app.globalData.records.wx_public_open_id){
                          wx.redirectTo({
                            url: '/pages/webview/index?id='+res.data.data.records.id,
                          })
                      }
                      wx.switchTab({
                        url: '/pages/index/index',
                      })
                    }
                  },
                  fail: function() {
                    wx.switchTab({
                      url: '/pages/index/index',
                    })
                    console.log("请求失败")
                  }
                })



						}
          })


				} else {
					wx.showToast({
						title: res.data.msg,
						icon: 'none',
						duration: 2000
					})
				}
			},
			fail: function(res) {
				wx.hideLoading()
				wx.showToast({
				  title: '登录失败',
				  icon: 'none',
				  duration: 2000
				})
				console.log("请求失败")
			}
		})
  },
  verifyCodeChange(e){
      this.setData({
        verify_code:e.detail.value
      })
  },
	//验证码登录
	onCodeLogin(e) {

		let phone = this.data.phone2;
		let verify_code =  this.data.verify_code;

		if (!phone || phone.length <= 0) {
			wx.showToast({
				title: "请输入手机号",
				icon: 'none',
				duration: 2000
			})
			return false
		}
		if (!verify_code || verify_code.length <= 0) {
			wx.showToast({
				title: "请输入验证码",
				icon: 'none',
				duration: 2000
			})
			return false
		}
		if (!(/^1[345789]\d{9}$/.test(phone))) {
			wx.showToast({
				title: "请输入正确的手机号",
				icon: 'none',
				duration: 2000
			})
			return false
		}
		wx.showLoading({
			title: '提交中',
			mask: true
		})

		wx.request({
			url: app.globalData.baseUrl + '/api/loginWithVerifyCode',
			method: 'POST',
			header: {
				'content-type': 'application/x-www-form-urlencoded',
			},
			data: {
				"phone": phone,
				"verify_code": verify_code
			},
			success: function(res) {
				wx.hideLoading()
				if (res.data.status) {
					app.globalData.token = res.data.data.records.token;
					// app.globalData.is_finish_wx = res.data.data.records.is_finish_wx;
					// app.globalData.phone = res.data.data.records.phone;
					wx.setStorage({
						key: "time",
						data: new Date().getTime(),
						success() {

						}
					})
					wx.setStorage({
						key: "token",
						data: res.data.data.records.token,
						success() {
							// //请求个人信息，判断是否跳转到webview授权页面
              wx.request({
                url: app.globalData.baseUrl + '/api/user/getUserInfo',
                method: 'GET',
                header: {
                  'content-type': 'application/x-www-form-urlencoded',
                  'token': res.data.data.records.token,
                },
                success: function(res) {
                  if (res.data.status) {
                    app.globalData.records = res.data.data.records;
                    wx.setStorage({
                      key: "records",
                      data: JSON.stringify(res.data.data.records)
                    })
                    if(!app.globalData.records.wx_public_open_id){
                        wx.redirectTo({
                          url: '/pages/webview/index?id='+res.data.data.records.id,
                        })
                    }
                    wx.switchTab({
                      url: '/pages/index/index',
                    })
                  }
                },
                fail: function() {
                  wx.switchTab({
                    url: '/pages/index/index',
                  })
                  console.log("请求失败")
                }
              })
						}
					})
					// wx.setStorage({
					//   key:"is_finish_wx",
					//   data:res.data.data.records.is_finish_wx,
					//   success(){

					//   }
					// })
					// wx.setStorage({
					//   key:"phone",
					//   data:res.data.data.records.phone,
					//   success(){
					//     wx.switchTab({
					//       url: '/pages/index/index',
					//     })
					//   }
					// })
				} else {
					wx.showToast({
						title: res.data.msg,
						icon: 'none',
						duration: 2000
					})
				}
			},
			fail: function() {
				wx.hideLoading()
				console.log("请求失败")
			}
		})
	},
	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function(options) {

	},

	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady: function() {

	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow: function() {

		if (wx.canIUse('hideHomeButton')) {

			wx.hideHomeButton()

		}

	},

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide: function() {

	},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload: function() {

	},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh: function() {

	},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom: function() {

	},

	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage: function() {

	}
})
